import type { ObjectDirective } from 'vue'
import { useAuth } from '@/hooks/auth'

export const auth: ObjectDirective = {
  mounted(el: HTMLButtonElement, binding) {
    if (binding.value == undefined) return

    const val = binding.value
    let { funcKey, effect } = { funcKey: '', effect: '' }
    if (typeof val === 'string') {
      funcKey = val
    } else {
      funcKey = val.funcKey
      effect = val.effect
    }

    // 如果funcKey不传，则认为不需要授权认证
    if (funcKey == undefined) {
      return
    }
    const authHook = useAuth()
    if (!authHook.hasFuncKey(funcKey)) {
      if (effect == 'disabled') {
        el.disabled = true
        el.setAttribute('title', '没有权限')
      } else {
        //默认处理 删除
        el.remove()
      }
    }
  }
}
